﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Receiver
{
    public partial class Form : FormPage
    {
        BLL.Receiver bll = new BLL.Receiver();
        Model.Receiver model = new Model.Receiver();
        //
        BLL.PAUser bllUser = new BLL.PAUser();
        Model.PAUser modelUser = new Model.PAUser();
        protected void Page_Load(object sender, EventArgs e) { }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel() { model = bll.GetModel(Master.id); modelUser = bllUser.GetModel(model.UserID); }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            tb_Password_helper.Visible = true;

            tb_Realname.Text = model.Realname;
            tb_Mobile.Text = model.Mobile;

            cb_IsLock.Checked = modelUser.IsLock;
        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            string Realname = PageValidate.SqlTextClear(DataTypePlus.requestString(tb_Realname.Text));
            string Password = PageValidate.SqlTextClear(tb_Password.Text); ;
            string mobile = PageValidate.SqlTextClear(tb_Mobile.Text);

            if (
                Master.noEmpty(Realname, "[姓名]必须填写") &&
                Master.noChongfu("Realname='" + Realname + "'", "[姓名]重复")
                )
            {
                modelUser.Username = Realname;
                modelUser.IsLock = cb_IsLock.Checked;
                modelUser.Mobile = mobile;

                model.Realname = Realname;
                model.Mobile = mobile;
                if (Master.formState == EnumHelper.FormState.add)
                {
                    //添加User
                    modelUser.Password = StringPlus.md5(Password);
                    modelUser.UserType = (int)EnumHelper.UserType.Receiver;
                    modelUser.State = (int)EnumHelper.UserState.Nomal;
                    int UserID_new = bllUser.Add(modelUser);
                    //添加Receiver
                    model.UserID = UserID_new;
                    //Rank
                    List<Model.Receiver> listModelReceiver = bll.GetModelList(" id>0 order by Rank desc");
                    if (listModelReceiver != null && listModelReceiver.Count > 0) model.Rank = listModelReceiver[0].Rank + 1;
                    else model.Rank = 1;
                    //Number
                    model.Number = "B"+model.Rank.ToString().PadLeft(2, '0');//2
                    Master.id = bll.Add(model);
                }
                else
                {
                    if (Password != "") modelUser.Password = StringPlus.md5(Password);
                    bllUser.Update(modelUser);
                    bll.Update(model);
                }

                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Receiver, Master.id, Master.formState == EnumHelper.FormState.add ? EnumHelper.OperateType.Add : EnumHelper.OperateType.Update, "");
                
                Master.SubmitOK();

            }
        }
    }
}